package jee.boot.data.permission.execute;

import com.google.common.collect.Lists;
import jee.boot.common.basic.DataEntity;
import jee.boot.common.basic.interceptor.PostQueryInterceptor;
import jee.boot.entity.auth.AuthEntity;
import jee.boot.entity.auth.StandardModel;
import jee.boot.entity.auth.standard.DataValue;

import java.util.List;
import java.util.Map;

/**
 * 针对所有StandardMode的查询后的数据增加权限设置
 */
public class PostQueryExecuteInterceptor extends BasicExecute implements PostQueryInterceptor {
    /**
     * 执行前置操作
     *
     * @param entity 查询的参数
     * @param data   返回的数据
     */
    @Override
    public void execute(DataEntity<?> entity, Object data) {
        if (StandardModel.class.equals(entity.getClass())) {
            StandardModel standardModel = (StandardModel) entity;
            List<AuthEntity> rowSetPermission = standardModel.getRowSetPermission();
            List<AuthEntity> cellSetPermission = standardModel.getCellSetPermission();
            if (data instanceof Map) { // get 查询
                Map<String, Object> map = (Map<String, Object>) data;
                Map<String, DataValue> dataValueMap = getDataValuePermission(map, rowSetPermission, cellSetPermission);
                standardModel.setData(dataValueMap);
            } else if (data instanceof List) {// list查询
                List<Map<String, Object>> listData = (List<Map<String, Object>>) data;
                if (listData != null && !listData.isEmpty()) {
                    standardModel.setList(Lists.newArrayList());
                    for (Map<String, Object> map : listData) {
                        Map<String, DataValue> dataValueMap = getDataValuePermission(map, rowSetPermission, cellSetPermission);
                        standardModel.getList().add(dataValueMap);
                    }
                }
            }

        }

    }


}
